<?php
class m_kriteria extends Model{
    var $table = 'kriteria';
    
    function getCountRows(){
        return $this->db->count_all($this->table);
    }
    function getListKriteria($offset = NULL,$limit = NULL){
        if($offset != NULL || $limit != NULL){
            $limits = "limit $offset,$limit";
        }else $limits = '';
        
        $query = $this->db->query("select * from kriteria $limits");
        if($query->num_rows() > 0){
           $result = $query->result_array();    
        }else $result = array();
        
        return $result;
    }
    function getKriteriaById($id = NULL){
        if($id != NULL){
            $where = "where id = '$id'";
        }else $where = "";
            
        $result = array();
        $query = $this->db->query("select * from kriteria $where");
        if($query->num_rows() > 0){
           $result = $query->result_array();    
        }else $result = array();
        
        return $result;
    }
    function addKriteria($data){
        $insert = $this->db->insert($this->table,$data);
        $id = $this->db->insert_id();
        //jika ditambahkan kriteria baru maka input tabel bobot_alternatif dengan data calon adik asuh
        $query = $this->db->query("select id from adik_asuh where status = '0'");
        if($query->num_rows() > 0){
            $result = $query->result_array();
            foreach ($result as $res){
                $input = $this->db->query("insert into bobot_alternatif (id_kriteria,id_calon_adik_asuh,bobot) values ('$id','$res[id]',0)");
            }
        }
        if(!$insert){
            return FALSE;
        }else return TRUE;
    }
    function editKriteria($data){
        $this->db->where('id',$data['id']);
        $update = $this->db->update($this->table,$data);
        if(!$update){
            return FALSE;
        }else return TRUE;
    }
    function deleteKriteria($id){
        $delete = $this->db->delete($this->table,array('id'=>$id));
        if(!$delete){
            return FALSE;
        }else return TRUE;
    }
}
?>
